//
//  WSBaseRefreshPageCustomTableViewController.m
//  WSFramework
//
//  Created by zhangcong on 2018/4/19.
//  Copyright © 2018年 zhangcong. All rights reserved.
//

#import "WSBaseRefreshPageCustomTableViewController.h"
//#import "UIViewController+WSRefreshNew.h"
#import <WSLoadMoreView.h>
#import <MJRefresh.h>

@interface WSBaseRefreshPageCustomTableViewController ()

@property (assign,nonatomic) NSInteger pageIndex;
@property (assign,nonatomic) BOOL isLoadMore;
@end

@implementation WSBaseRefreshPageCustomTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   
    [self setupLoadMore];
}

- (void)setupLoadMore {
    WS(weakSelf)
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        SS(strongSelf)
        [strongSelf didStartLoadMore];
    }];
    self.tableView.mj_footer.hidden = YES;
//    if ([self autoRefreshLoad]) {
//        [self.tableView.mj_header beginRefreshing];
//    }
}

- (void)didRequest {
//    if( [self isLoadData] ) {
//        [self setIsLoadData:NO];
//        _pageIndex = 1;
//        [self setIsLoadMoreError:NO];
//        [self setIsLoadMore:YES];
//        [self didRequest:_pageIndex];
//    }
    [self.tableView.mj_footer resetNoMoreData];
    self.isLoadMore = NO;
    _pageIndex = 1;
    self.tableView.mj_footer.hidden = YES;
    [self didRequest:_pageIndex];
}

- (void)didRequest:(NSInteger)pageIndex {
    
}

- (void)didStartLoadMore {
    ++_pageIndex;
    self.isLoadMore = YES;
    [self.tableView.mj_footer beginRefreshing];
    [self didRequest:_pageIndex];
}



- (void)didRequestError:(NSError *)error {
    if (!(_pageIndex == 1) ) {
        --_pageIndex;
//        [self errorLoadMore];
    }
    [super didRequestError:error];
}

- (void)didRequestComplete:(id)data {
    
    NSInteger count = [(NSArray *)data count];
    if (_pageIndex == 1) {
        _dataArray = [[NSMutableArray alloc] initWithArray:data];
        [self endRefreshing];
        
        if (count == 0) {
            [self.tableView.mj_footer setHidden:YES];
        }
    }
    else
    {
        if ( count > 0  ) {
            [self performSelector:@selector(endLoadMore) withObject:nil afterDelay:0.5];
            [_dataArray addObjectsFromArray:data];
            
        }
        else {
            [self notLoadMore];
        }
    }
    
    [super didRequestComplete:_dataArray];
    [self handRefreshFooter];
}

- (void)endLoadMore {
    [self.tableView.mj_footer endRefreshing];
}

- (void)notLoadMore {
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
}

- (void)handRefreshFooter{
    RunAsyncOnMainThread(
//        if (self.tableView.mj_footer.state != MJRefreshStateNoMoreData) return; //过滤 不是 没有更多数据footer
        if (!self.tableView.mj_footer ) return;
        
        CGFloat insetTop = self.tableView.mj_header.isRefreshing ? 0 : self.tableView.contentInset.top;
        CGFloat y = [self.tableView convertRect:self.tableView.bounds toView:self.tableView.window].origin.y;
        CGFloat tableBottom = self.tableView.contentSize.height + insetTop + self.tableView.contentInset.bottom + y;
        self.tableView.mj_footer.hidden = tableBottom < [[UIScreen mainScreen] bounds].size.height;
    )
}

- (void) dealloc {
//    [self removeLoadMore];
}

@end
